package com.ecarx.library_base.base_view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.ecarx.library_base.base_util.DebounceClickListener;

/**
 * @author : kjn
 * @date : 19-5-7 上午10:18
 * Description : 触摸时显示50%的透明度,根布局拦截触摸点击事件，子控件监听点击事件无效
 */
public class ThemeConstraintLayout extends ConstraintLayout {

    private boolean isActionDown;


    public ThemeConstraintLayout(Context context) {
        super(context);
    }

    public ThemeConstraintLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ThemeConstraintLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        isActionDown = event.getAction() == MotionEvent.ACTION_DOWN;
        return super.onTouchEvent(event);
    }

    @Override
    protected void dispatchSetPressed(boolean pressed) {
        if (this.isClickable() && pressed && isActionDown) {
            setAlpha(0.5f);
        } else {
            setAlpha(1f);
        }
        isActionDown = false;
        super.dispatchSetPressed(pressed);
    }

    @Override
    public void setOnClickListener(@Nullable OnClickListener l) {
        DebounceClickListener l1 = new DebounceClickListener(l);
        super.setOnClickListener(l1);
    }
}
